function getDate(mth) {
    return '2017年' + mth.toString() + '月';
}

function showData(mth) {
    $('.x-mask').hide();
    $('#msk' + mth.toString()).show();
    var src = dict[mth];
    var record = $('.record');
    record.children('.entry').remove();
    var sumcost = 0;
    for(var i = 0; i < src.length; i++) {
        sumcost += src[i][3];
    }
    for(var i = 0; i < src.length; i++) {
        var cur = src[i];
        var entry = $('<div></div>').addClass('entry');

        var entry_image =  $('<div></div>').addClass('entry_image');
        var img = $('<img>').addClass('entry_icon').attr('src', '/static/ecard/img/' + cur[1]);
        entry_image.append(img);
        entry.append(entry_image);

        var entry_middle = $('<div></div>').addClass('entry_middle');
        var entry_item = $('<div></div>').addClass('entry_item').text(cur[0]);
        var entry_amount = $('<div></div>').addClass('entry_amount').text('￥' + cur[3]);
        entry_middle.append(entry_item);
        entry_middle.append(entry_amount);
        entry.append(entry_middle);

        var entry_percentage = $('<div></div>').addClass('entry_percentage');
        var per = sumcost > 0 ? (cur[3] / sumcost * 100.0).toFixed(1) : 0.0;
        entry_percentage.text(per.toString() + '%');
        entry.append(entry_percentage);

        record.append(entry);
    }
}

function showMask() {
    var mask = $('#mask');
    mask.css("height", $(document).height());
    mask.css("width", $(document).width());
    mask.fadeIn();
}

function hideMask() {
    $("#mask").fadeOut();
}

function drawCurve() {
    for (var i = 0; i < pointx.length; ++i) {
        pointx[i] = pointx[i] / 300 * canvas_width;
        pointy[i] = pointy[i] / 250 * canvas_height;
    }
    for (var i = 0; i < cpointx.length; ++i) {
        cpointx[i] = cpointx[i] / 300 * canvas_width;
        cpointy[i] = cpointy[i] / 250 * canvas_height;
    }
    var test = document.getElementById('test');
    test.width = canvas_width;
    test.height = canvas_height;
    test = test.getContext('2d');
    test.beginPath();
    test.lineWidth = "3";
    for (var i = 1; i < pointx.length - 1; ++i) {
        test.moveTo(pointx[i], pointy[i]);
        index = (i - 1) * 2 + 1;
        test.bezierCurveTo(cpointx[index], cpointy[index], cpointx[index + 1], cpointy[index + 1], pointx[i + 1], pointy[i + 1]);
    }
    test.strokeStyle = '#fff';
    test.stroke();

    for (var i = 1; i < pointx.length - 1; ++i) {
        test.beginPath();
        test.fillStyle = "#fff";
        test.arc(pointx[i], pointy[i], 4, 0, 2 * Math.PI);
        test.fill();
    }

    for (var i = 1; i < pointx.length - 1; ++i) {
        test.font = '1rem microsoft yahei'
        test.font = 'bold 1rem microsoft yahei';
        test.fillText(amount[i - 1], pointx[i] - 7, pointy[i] - 20);
    }
}

var dict = {};
var dateToMonth = {};

function prepare() {
    var curmonth = 0;
    var buf = [];
    for(var i = 0; i < stat_list.length; i++) {
        if(i % 6 == 0 && i > 0) {
            dict[months[curmonth]] = buf;
            buf = [];
            curmonth++;
        }
        buf.push(stat_list[i]);
    }
    dict[months[curmonth]] = buf;
    for(var i = 0; i < months.length; i++) {
        dateToMonth[getDate(months[i])] = months[i];
    }
}



